package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.service.admin.MarketCouponService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName AdminCouponController
 * @Description TODO 优惠券管理 功能未完善！！！
 * @Author MZ
 * @Date 2023/5/10 8:51
 */
@RequiresAuthentication
@RestController
@RequestMapping("/admin/coupon")
@Validated
public class AdminCouponController {
    private final Log logger = LogFactory.getLog(AdminCouponController.class);

    @Autowired
    private MarketCouponService couponService;

    // page=1&limit=20&name=666&type=0&status=0&sort=add_time&order=desc
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String page, String limit, String name, String type,String status, String sort, String order) {
        // 接收请求参数
        // 对参数添加默认值
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        Integer pageInt = null;
        Integer limitInt = null;
        try {
            pageInt = Integer.parseInt(page);
            limitInt = Integer.parseInt(limit);
        }catch (Exception e) {
            return ResponseUtil.badArgument();
        }
        // 校验成功，进行查询操作
        List<MarketCoupon> marketCouponsList = couponService.list(pageInt, limitInt, name, type, status, sort, order);
        // 返回结果
        return ResponseUtil.okList(marketCouponsList);
    }

    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody MarketCoupon marketCoupon) throws IOException {

        boolean result = valid(marketCoupon);
        if(!result){
            //校验没有通过
            return ResponseUtil.badArgument();
        }
        // 添加数据库，并且返回完整bo对象
        couponService.addCoupon(marketCoupon);

        System.out.println("combineBo = " + marketCoupon);

        return ResponseUtil.ok(marketCoupon);
    }

    /**
     * 判空操作
     * @param marketCoupon
     * @return
     */
    public boolean valid(MarketCoupon marketCoupon){
        // 判断优惠券名称是否为空
        if (StringUtils.isEmpty(marketCoupon.getName())){
            return false;
        }
        return true;
    }

    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "详情")
    @GetMapping("/read")
    public Object read( String id) {

        if (id == null || id.equals("")) {
            return ResponseUtil.badArgument();
        }
        MarketCoupon result = couponService.read(id);
        return ResponseUtil.ok(result);
    }

    @RequiresPermissions("admin:coupon:list")
    @GetMapping("/listuser")
    public Object listUser(String page,String limit,String couponId,String sort,String order ) {

        List<MarketCoupon> result = couponService.listUser(page, limit, couponId, sort, order);

        return ResponseUtil.okList(result);
    }

    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MarketCoupon marketCoupon) {

        String name = marketCoupon.getName();
        // 参数校验
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        // 获取基于领取时间的有效天数days
        Short days = marketCoupon.getDays();
        // 获取addTime
        LocalDateTime addTime = marketCoupon.getAddTime();
        if (days != null) {
            // 获取days天后的LocalDateTime对象
            LocalDateTime endTime = addTime.plusDays(days);
            // 设置属性
            marketCoupon.setEndTime(endTime);
        }
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 设置updateTime
        marketCoupon.setUpdateTime(now);

        //根据resCode是否是1还是0判断广告管理信息是否更新成功
        int resCode = couponService.update(marketCoupon);
        if (resCode == 0) {
            // 更新失败
            Object o = ResponseUtil.updatedDataFailed();
            return o;
        }
        // 更新成功
        // 设置响应体
        return ResponseUtil.ok(marketCoupon);
    }

    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketCoupon marketCoupon) {

        String name = marketCoupon.getName();
        // 参数校验
        if (StringUtils.isEmpty(name)) {
            Object o = ResponseUtil.badArgument();
            return o;
        }

        // 获取基于领取时间的有效天数days
        Short days = marketCoupon.getDays();
        // 获取addTime
        LocalDateTime addTime = marketCoupon.getAddTime();
        if (days != null) {
            // 获取days天后的LocalDateTime对象
            LocalDateTime endTime = addTime.plusDays(days);
            // 设置属性
            marketCoupon.setEndTime(endTime);
        }
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 设置updateTime
        marketCoupon.setUpdateTime(now);

        //根据resCode是否是1还是0判断广告管理信息是否更新成功
        int resCode = couponService.delete(marketCoupon);
        if (resCode == 0) {
            // 删除失败
            return ResponseUtil.updatedDataFailed();
        }
        // 删除成功
        // 设置响应体
        return ResponseUtil.ok(marketCoupon);
    }

}
